package com.cjl.demo.tznew.test;

import com.alibaba.fastjson.JSON;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author wangxiaoxi
 * @date 2022/01/11
 */
public class AccessFileScanUtil {
    /**
     * 关键词路径
     */
    public final static String FILE_PATH = "/Users/jinliangchen/Downloads/params_data.txt";
    private static Map<String, String> pathSet = new HashMap<>();

    /**
     * 初始化单词到Reids中
     *
     * @return
     */
    public static void scan() throws IOException {

        File file = new File(FILE_PATH);
        FileInputStream inputStream = null;
        Scanner sc = null;
        try {
            inputStream = new FileInputStream(file.getPath());
            sc = new Scanner(inputStream, "UTF-8");

            int i = 0;
            while (sc.hasNextLine()) {
                if (i == 0) {
                    i++;
                    continue;
                }
                if (i % 1000 == 0) {
                    System.out.println("Finished " + i++);
                }
                String line = sc.nextLine();
                String[] split = line.split("\\?");
                if (split.length == 2) {
                    String path = split[0];
                    String value = split[1];
                    // 过滤动态路径
                    if (path.startsWith("tzcapp.58.com/download/")) {
                        continue;
                    }
                    pathSet.put(path, value);
                } else {
                    String errMsg = "scan error data. " + line;
                    System.out.println(errMsg);
                }
            }
            System.out.println("结果Json：" + JSON.toJSONString(pathSet));
            if (sc.ioException() != null) {
                throw sc.ioException();
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (sc != null) {
                sc.close();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        scan();
    }
}
